package com.fengxiaoxiaoxi.fengutils.utils.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "fengxiaoxiaoxi.jwt.config")
public class JwtUtils {
    private String secretKey ;
    private long expires;

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public long getExpires() {
        return expires;
    }

    public void setExpires(long expires) {
        this.expires = expires;
    }

    public String  creaetToke(String id ,String subject ,Map<String ,Object> ... maps){
        long l = System.currentTimeMillis();


        JwtBuilder jwtBuilder = Jwts.builder().setId(id) //id
                .setIssuedAt(new Date(l)) //生产token 时间
                .setSubject(subject) // 主题
                .signWith(SignatureAlgorithm.HS256, secretKey); // 秘钥加密

        System.out.println(maps.length);
        //可变参数map , 当有map 是将第一个map 传入 token中
        if (maps.length!=0){
            Map<String, Object> map = maps[0];
            jwtBuilder =  jwtBuilder.setClaims(map);
        }
        return jwtBuilder.compact();
    }

    public Claims parserToken(String token){
        Claims body = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody();
        return body;
    }
}
